/*
 * sNode.h
 *
 *  Created on: 17 févr. 2011
 *      Author: seeme
 *      Un conteneur pour représenter un graph directionel.
 *      Certainement des choses à améliorer...
 */

#ifndef SNODE_H_
#define SNODE_H_

#include <iterator>
#include <list>
#include <iostream>

template <class T>
class sNode {
public:
	sNode<T>(){m_value = 0;}

	sNode<T>(T value):m_value(value){};

	sNode<T>(T val, std::list<T> children):m_value(val), m_children(children){};

	void setValue(T value){
		m_value = value;
	}

	T getValue(){
		return m_value;
	}

	void addChild(sNode<T>* child){
		m_children.push_back(child);
	}

	std::list<sNode<T>* > getChildren(){
		return m_children;
	}

	void setChildren(std::list<sNode<T> > children){
		m_children.clear();
		m_children = children;
	}

	void appendChildren(std::list<sNode<T> > children){
		m_children.insert(children);
	}

	int numberOfChildren(){
		return m_children.size();
	}

	friend std::ostream& operator<<(std::ostream& out, const sNode<T>& node){
		node.print(out);
		return out;
	}

	virtual void print(std::ostream& o) const {
		display(o);
	}

	void display(std::ostream& o, int depth=0) const{
		std::string spacer("");
		if(depth == 0)
			spacer += "\n";
		for(int i = 0; i < depth; ++i){
			spacer += "\t";
		}

		o << spacer << "<" << *m_value << ">" << std::endl;
		typedef typename std::list<sNode<T>* >::const_iterator Iterator;
		Iterator iter= m_children.begin();

		while(iter != m_children.end()){
			((sNode<T>*)*iter)->display(o, depth+1);
			iter++;
		}
	}

protected:
	T m_value;
	std::list<sNode<T>* > m_children;
};

#endif /* SNODE_H_ */
